//
//  PPFileInfo.m
//  FileView-Simple
//
//  Created by 甘文鹏 on 2018/4/26.
//  Copyright © 2018年 ganwenpeng.com. All rights reserved.
//

#import "PPFileInfo.h"

@implementation PPFileInfo
- (void)setFileSize:(NSInteger)fileSize {
    _fileSize = fileSize;
    
    NSInteger byte = fileSize;
    NSInteger kb = byte >> 10;
    NSInteger mb = kb >> 10;
    NSInteger gb = mb >> 10;
    
    if (byte < 1024) {
        _fileSizeText = [NSString stringWithFormat:@"%ldB", byte];
        return;
    }
    
    if (kb < 1024) {
        byte = byte - kb * 1024;
        if (byte > 0) {
            _fileSizeText = [NSString stringWithFormat:@"%.1f", kb+byte/1024.0];
        }
        _fileSizeText = [NSString stringWithFormat:@"%@KB", _fileSizeText];
        return;
    }
    
    if (mb < 1024) {
        kb = kb - mb * 1024;
        if (kb > 0) {
            _fileSizeText = [NSString stringWithFormat:@"%.1f", mb + kb/1024.0];
        }
        _fileSizeText = [NSString stringWithFormat:@"%@MB", _fileSizeText];
        return;
    }
    
    mb = mb - gb * 1024;
    if (mb > 0) {
        _fileSizeText = [NSString stringWithFormat:@"%.1f", gb + mb/1024.0];
    }
    _fileSizeText = [NSString stringWithFormat:@"%@GB", _fileSizeText];
    return;
}

- (instancetype)initWithFileName:(NSString *)fileName fileSize:(NSInteger)fileSize {
    if (self = [super init]) {
        _fileName = fileName;
        _fileSize = fileSize;
    }
    return self;
}

+ (instancetype)fileWithFileName:(NSString *)fileName fileSize:(NSInteger)fileSize {
    PPFileInfo *file = [[PPFileInfo alloc] init];
    
    file.fileName = fileName;
    file.fileSize = fileSize;
    return file;
}
@end
